﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.individualinfo
{
    public partial class rework_password : System.Web.UI.Page
    {
        YXShop.BLL.Member.MemberAccount memberBll = new YXShop.BLL.Member.MemberAccount();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitWebControl();
                if (Session["MemberID"] != null)
                {
                    string memberId = Session["MemberID"].ToString();
                    ViewState["memberId"] = memberId;
                }
                
            }
        }

        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtOldPass, "请出入旧密码", "isnull_6", "必填", "该项为必填项，长度至少6位");
            ChangeHope.WebPage.WebControl.Validate(this.txtNewPass, "请输入新密码", "isnull_6", "必填", "该项为必填项，长度至少6位");
            ChangeHope.WebPage.WebControl.Validate(this.txtRePass, "请再输入新密码", "isnull_6", "必填", "该项为必填项，长度至少6位");
           this.Form.Attributes.Add("onsubmit", "return CheckForm();");
        }


        protected void btnOk_Click(object sender, EventArgs e)
        {
            string oldPass = ChangeHope.Common.DEncryptHelper.Encrypt(this.txtOldPass.Text.Trim().ToString(),1);
            string newPass = this.txtNewPass.Text.Trim().ToString();
            string newRePass = this.txtRePass.Text.Trim().ToString();
            if(oldPass.Length<6||newPass.Length<6||newRePass.Length<6)
            {
                this.ltlMsg.Text = "输入密码最少6位";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            int id=0;
            if(ViewState["memberId"]!=null){
                id=Convert.ToInt32(ViewState["memberId"]);
            }
            YXShop.Model.Member.MemberAccount model=memberBll.GetModel(id);
            if(model!=null)
            {
                if (oldPass.Equals(model.PassWord))
                {
                    if (newPass.Equals(newRePass))
                    {
                        model.PassWord = newPass;
                        memberBll.Update(model);
                        this.ltlMsg.Text = "修改密码成功";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                        return;
                    }
                    else
                    {
                        this.ltlMsg.Text = "您输入的新密码两次不一致";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                else
                {
                    this.ltlMsg.Text = "您输入的旧密码有误";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
                
        }
    }
}
